package com.spring.sunflower.main.fragment;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.spring.sunflower.R;
import com.spring.sunflower.anchor.PersonalDetailActivity;
import com.spring.sunflower.base.BaseFragment;
import com.spring.sunflower.bean.NewestBean;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.main.MainContract;
import com.spring.sunflower.main.adapter.NewestAdapter;
import com.spring.sunflower.main.presenter.NewestPresenter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/5/28<br/>
 * <p>
 * Describe:<br/>
 */
public class NewestFragment extends BaseFragment<NewestPresenter> implements MainContract.NewestView, OnLoadMoreListener, OnRefreshListener {
    private SmartRefreshLayout refreshLayout;
    private RecyclerView mRecyclerView;
    private List<NewestBean.DatasBean> mData = new ArrayList<>();
    private NewestAdapter mAdapter;
    private int pageIndex = 1;

    @Override
    public int setLayoutResId() {
        return R.layout.fragment_nearby;
    }

    @Override
    public void initView() {
        refreshLayout = mView.findViewById(R.id.refreshLayout);
        mRecyclerView = mView.findViewById(R.id.rvRecyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new NewestAdapter(R.layout.adapter_nearby, mData);
        mRecyclerView.setAdapter(mAdapter);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void initData() {
    }

    @Override
    public void initListener() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                NewestBean.DatasBean datasBean = mData.get(position);
                Intent intent = new Intent(getActivity(), PersonalDetailActivity.class);
                intent.putExtra(Constant.KEY_TARGET_ID, String.valueOf(datasBean.getMemberId()));
                startActivity(intent);
            }
        });
    }

    @Override
    public void loadData() {
        mPresenter.getNewest(1);
    }

    @Override
    public NewestPresenter setPresenter() {
        return new NewestPresenter(this);
    }


    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadMore();
        }
        mPresenter.getNewest(++pageIndex);
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        }
        pageIndex = 1;
        mPresenter.getNewest(1);
    }

    @Override
    public void showNewestData(List<NewestBean.DatasBean> data) {
        if (pageIndex == 1) {
            mData.clear();
            mAdapter.setList(mData);
        }
        mAdapter.addData(data);
    }
}
